<template>
	<view class="">
		<view class="nav_top-bg">
			<u-navbar title="申请代理">
				<view class="u-nav-slot" slot="left">
					<u-icon name="arrow-left" @click="go_black" size="19"></u-icon>
					<u-line direction="column" :hairline="false" length="16" margin="0 10px"></u-line>
					<u-icon name="home" @click="goHome" size="20"></u-icon>
				</view>
			</u-navbar>
		</view>
		<view class="w-690">
			<view class="apply_leibie">
				<view class="d-flex">
					<text class="xing">*</text>
					<text class="touxtxt">申请类别</text>
				</view>
				<view class="righttx">
					<u-radio-group v-model="radiovalue1" placement="row">
						<u-radio :customStyle="{marginRight: '10px'}" v-for="(item, index) in radiolist1" :key="index"
							:label="item.name" :name="item.id" @change="radioChange">
						</u-radio>
					</u-radio-group>
				</view>
			</view>
			<view class="">
				<view class="avatar" v-if="radiovalue1 == 1">
					<view class="d-flex">
						<text class="xing">*</text>
						<text class="touxtxt">企业名称</text>
					</view>
					<view class="righttx">
						<input class="ipt" type="text" placeholder="请输入企业名称" v-model="company_name">
					</view>
				</view>
				<view class="avatar">
					<view class="d-flex">
						<text class="xing">*</text>
						<text class="touxtxt">联系人</text>
					</view>
					<view class="righttx">
						<input class="ipt" type="text" placeholder="请输入姓名" v-model="name">
					</view>
				</view>
				<view class="avatar">
					<view class="d-flex">
						<text class="xing">*</text>
						<text class="touxtxt">电话</text>
					</view>
					<view class="righttx">
						<input class="ipt" type="text" placeholder="请输入电话" v-model="phone">
					</view>
				</view>
				<view class="avatar">
					<view class="d-flex">
						<text class="xing">*</text>
						<text class="touxtxt">意向地区</text>
					</view>
					<view class="righttx d-flex align-items" @tap="chooseCity()">
						<view :class="adress == '请选择' ? 'top-txt' : 'top-txt-xz'">{{adress}}</view>
						<image class="qm" src="../../static/index/more.png" mode="widthFix"></image>
					</view>
				</view>
				<view class="zhizhao"  v-if="radiovalue1 == 0">
					<text class="touxtxt">上传证件</text>
					<view class="shenfenz d-flex align-items justify-content-space-between">
						<view class="">
							<u-upload  :fileList="fileList2" @afterRead="afterRead" @delete="deletePic" name="2" multiple
								:maxCount="1" width="318rpx" height='204rpx'>
								<image :src="src" class="sfz_img" mode="widthFix" v-if="avatar"></image>
								<image src="../static/sfz.png" class="sfz_img" mode="widthFix" v-else></image>
							</u-upload>
						</view>
						<view class="">
							<u-upload :fileList="fileList3" @afterRead="afterRead" @delete="deletePic" name="3" multiple
								:maxCount="1" width="318rpx" height='204rpx'>
								<image :src="src" class="sfz_img" mode="widthFix" v-if="avatar"></image>
								<image src="../static/sfz_f.png" class="sfz_img" mode="widthFix" v-else></image>
							</u-upload>
						</view>
					</view>
				</view>
				<view class="zhizhao" v-if="radiovalue1 == 0">
					<text class="touxtxt">上传照片</text>
					<view class="adress_suozai">
						<u-upload width="136rpx" height="136rpx" :fileList="fileList4" @afterRead="afterRead" @delete="deletePic" name="4" multiple
							:maxCount="4">
							<image :src="src" class="shangchuanimg" v-if="avatar"></image>
							<image src="../static/upload.png" class="shangchuanimg" v-else></image>
						</u-upload>
					</view>
				</view>
				<view class="zhizhao" v-if="radiovalue1 == 1">
					<view class="d-flex">
						<text class="xing">*</text>
						<text class="touxtxt">营业执照</text>
					</view>
					<view class="adress_suozai">
						<u-upload width="136rpx" height="136rpx" :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
							:maxCount="1">
							<image :src="src" class="shangchuanimg" v-if="avatar"></image>
							<image src="../static/upload.png" class="shangchuanimg" v-else></image>
						</u-upload>
					</view>
				</view>
			</view>
		</view>
		<!-- 确定提交 -->
		<view class="foot_btn d-flex align-items justify-content-center">
			<view class="foot_btn-but">
				<u-button text="确定提交" size="large" @click="tijiao" color="#FF6400"></u-button>
			</view>
		</view>
		<u-picker :show="showcity" ref="uPicker" @cancel="cancel" :columns="columns" @confirm="confirm"
			@change="changeHandler" keyName="t_name"></u-picker>
	</view>
</template>

<script>
	import {
		tree
	} from '@/api/citys.js'
	import {
		upFile
	} from '@/utils/url.js'
	import {
		dailishenqing
	} from '@/api/user.js'
	export default {
		data() {
			return {
				radiolist1: [{
						id: 0,
						name: '个人',
						disabled: false
					},
					{
						id: 1,
						name: '企业',
						disabled: false
					}
				],
				radiovalue1: 0,
				fileList1:[],
				fileList2:[],
				fileList3:[],
				fileList4:[],
				avatar: '',
				adress: '请选择省/市',
				company_name:'',
				phone:'',
				name:'',
				src: require('../static/upload.png'),
				showcity: false,
				cityname: '',
				shiname: '',
				quname: '',
				shiid: '',
				shengid: '',
				columns: [],
			}
		},
		async onLoad() {
			await this.getProvinces()
		},
		methods: {
			go_black() {
				uni.navigateBack({
					delta: 1
				})
			},
			goHome() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			radioChange(e) {
				console.log("申请类别选择", e);
				this.radiovalue1 = e
			},
			chooseCity() {
				console.log("eeeee", this.columns);
				this.showcity = true
			},
			// 省市区
			changeHandler(e) {
				const {
					columnIndex,
					value,
					values,
					index,
					indexs,
					picker = this.$refs.uPicker
				} = e
				if (columnIndex === 0) {
					picker.setColumnValues(1, this.columns[0][index].children)
					// picker.setColumnValues(2, this.columns[0][index].children[0].children)
				}
				// if (columnIndex === 1) {
				// 	console.log(e, indexs);
				// 	// picker为选择器this实例，变化第二列对应的选项
				// 	picker.setColumnValues(2, this.columns[0][indexs[0]].children[indexs[1]].children)
				// }
				console.log("columnIndex", columnIndex);
			},
			confirm(e) {
				console.log("选择省市",e);
				this.cityname = e.value[0].name
				this.shengid = e.value[0].id
				this.shiname = e.value[1].name
				this.shiid = e.value[1].id
				this.adress = this.cityname + '/' + this.shiname
				this.showcity = false
			},
			cancel(e) {
				this.showcity = false
			},
			// 获取省市县接口数据
			async getProvinces() {
				var data = {
					pid: 0
				}
				await tree(data).then(res => {
					console.log("选择了第几个", res);
					if (res.code == 1) {
						this.columns[0] = res.data
						this.columns[1] = res.data[0].children
						// this.columns[2] = res.data[0].children[0].children
					}
			
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				var url = event.file[0].url
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				console.log("00000000000000", lists);
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						fullurl: result.fullurl,
						url_noyuming: result.url
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				var that = this
				console.log("url有没有值", url);
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: upFile,
						filePath: url,
						name: 'file',
						formData: {
							user: 'test',
							token: uni.getStorageSync('token')
						},
						success: (res) => {
							console.log("s111111111111111______", res);
							var data = JSON.parse(res.data);
							resolve(data.data)
						}
					});
				})
			},
			// 确认提交
			tijiao() {
				//证件图片
				let phone_arr = []
				for (let s of this.fileList4) {
					phone_arr.push(s.url_noyuming)
				}
				
				var data = {
					sqtype: this.radiovalue1,
					companyname: this.company_name,
					lianxiren: this.name,
					phone: this.phone,
					province: this.shengid,
					city: this.shiid,
					yyzz: this.fileList1.length ? this.fileList1[0].url_noyuming : '',
					sfzz:this.fileList2.length ? this.fileList2[0].url_noyuming : '',
					sfzb:this.fileList3.length ? this.fileList3[0].url_noyuming : '',
					zhaopian: phone_arr.join(','),
				}
				if(data.sqtype ==1) {
					// 代表企业
					if (!data.companyname) return uni.showToast({
						title: '请填写企业名称',
						icon: 'none',
					})
					if (!data.yyzz) return uni.showToast({
						title: '请上传营业执照',
						icon: 'none',
					})
				}
				
				if (!data.lianxiren) return uni.showToast({
					title: '请填写姓名',
					icon: 'none',
				})
				if (!data.phone) return uni.showToast({
					title: '请填写电话',
					icon: 'none',
				})
				if (!data.province && !data.city) return uni.showToast({
					title: '请选择省市',
					icon: 'none',
				})
				dailishenqing(data).then(res => {
					console.log("代理申请成功",res);
					if(res.code == 1){
						uni.showToast({
							title:'申请成功',
							icon:'success'
						})
						setTimeout(() => {
							uni.navigateTo({
								url: '/pagesB/daili_apply/daili_apply_status'
							})
						},500)
					}else {
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
				
			},
		}
	}
</script>

<style>
	.shenfenz {
		margin: 30rpx auto;
	}

	.sfz_img {
		width: 318rpx;
		height: 204rpx;
		display: block;
	}

	.foot_btn-but {
		width: 670rpx;
		margin: auto;
	}

	.foot_btn {
		width: 750rpx;
		position: fixed;
		bottom: 80rpx;
	}

	.top-txt-xz {
		font-size: 28rpx;
		color: #333;
	}

	.top-txt {
		font-size: 28rpx;
		color: #999999;
	}

	.qm {
		width: 12rpx;
		display: block;
		margin-left: 10rpx;
	}

	.ipt {
		font-size: 28rpx;
		font-weight: normal;
		text-align: right;
		color: #333333;
	}

	.shangchuanimg {
		width: 136rpx;
		height: 136rpx;
		border: 1px solid #eee;
		display: block;
		margin: auto;
		overflow: hidden;
	}

	.xing {
		color: red;
	}

	.adress_suozai {
		margin-top: 20rpx;
	}

	.zhizhao {
		width: 670rpx;
		margin: 30rpx auto;
	}

	.apply_leibie {
		width: 670rpx;
		height: 110rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin: auto;
	}

	.avatar {
		width: 670rpx;
		border-bottom: 1px solid #f5f5f5;
		height: 110rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin: auto;
	}

	.touxtxt {
		font-size: 30rpx;
		font-weight: normal;
		display: block;
		color: #333333;
	}
</style>